package com.example.demo.component;

import com.example.demo.service.AbstractHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Copyright 上海危网信息科技有限公司版权所有
 * @company wwsoft
 * @Date 2020/4/21 11:03
 */
@Component
public class HandlerOrderContext {
    @Resource
    private ApplicationContext applicationContext;
    /**
     * 存放所有策略类Bean的map
     */
    public static Map<String, Class<AbstractHandler>> orderStrategyBeanMap= new HashMap<String, Class<AbstractHandler>>(16);

    public AbstractHandler getOrderStrategy(Integer type){
        Class<AbstractHandler> strategyClass = orderStrategyBeanMap.get(type.toString());
        if(strategyClass==null){
            throw new IllegalArgumentException("没有对应的订单类型");
        }
        //从容器中获取对应的策略Bean
        return applicationContext.getBean(strategyClass);
    }

}
